package com.sugar.real.estate.module.business.controller.admin.openedcity;

import com.sugar.real.estate.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.framework.excel.core.util.ExcelUtils;
import com.sugar.real.estate.module.business.controller.admin.openedcity.vo.OpenedCityListReqVO;
import com.sugar.real.estate.module.business.controller.admin.openedcity.vo.OpenedCityRespVO;
import com.sugar.real.estate.module.business.controller.admin.openedcity.vo.OpenedCitySaveReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.openedcity.OpenedCityDO;
import com.sugar.real.estate.module.business.service.openedcity.OpenedCityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.real.estate.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 已开通城市")
@RestController
@RequestMapping("/business/opened-city")
@Validated
public class OpenedCityController {

    @Resource
    private OpenedCityService openedCityService;

    @PostMapping("/create")
    @Operation(summary = "创建已开通城市")
    @PreAuthorize("@ss.hasPermission('business:opened-city:create')")
    public CommonResult<Long> createOpenedCity(@Valid @RequestBody OpenedCitySaveReqVO createReqVO) {
        return success(openedCityService.createOpenedCity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新已开通城市")
    @PreAuthorize("@ss.hasPermission('business:opened-city:update')")
    public CommonResult<Boolean> updateOpenedCity(@Valid @RequestBody OpenedCitySaveReqVO updateReqVO) {
        openedCityService.updateOpenedCity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除已开通城市")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:opened-city:delete')")
    public CommonResult<Boolean> deleteOpenedCity(@RequestParam("id") Long id) {
        openedCityService.deleteOpenedCity(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得已开通城市")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:opened-city:query')")
    public CommonResult<OpenedCityRespVO> getOpenedCity(@RequestParam("id") Long id) {
        OpenedCityDO openedCity = openedCityService.getOpenedCity(id);
        return success(BeanUtils.toBean(openedCity, OpenedCityRespVO.class, o -> o.setName(String.format("%s (%s)", o.getNameZhCn(), o.getNameEnUs()))));
    }

    @GetMapping("/list")
    @Operation(summary = "获得已开通城市列表")
    @PreAuthorize("@ss.hasPermission('business:opened-city:query')")
    public CommonResult<List<OpenedCityRespVO>> getOpenedCityList(@Valid OpenedCityListReqVO listReqVO) {
        List<OpenedCityDO> list = openedCityService.getOpenedCityList(listReqVO);
        return success(BeanUtils.toBean(list, OpenedCityRespVO.class, o -> o.setName(String.format("%s (%s)", o.getNameZhCn(), o.getNameEnUs()))));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出已开通城市 Excel")
    @PreAuthorize("@ss.hasPermission('business:opened-city:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOpenedCityExcel(@Valid OpenedCityListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<OpenedCityDO> list = openedCityService.getOpenedCityList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "已开通城市.xls", "数据", OpenedCityRespVO.class,
                        BeanUtils.toBean(list, OpenedCityRespVO.class));
    }

}